import 'package:flutter/material.dart';
import 'package:gameplay_flutter/lib_baseview/theme/jr.dart';
import 'package:gameplay_flutter/lib_im/index.dart';
import 'package:get/get.dart';
import 'package:ones/ui.dart';

import '../../generated/assets.dart';
import '../../resource/language/generated/l10n.dart';
import 'sel_friend_logic.dart';

class SelFriendPage extends StatelessWidget {
  SelFriendPage({Key? key}) : super(key: key);

  final SelFriendLogic logic = Get.put(SelFriendLogic());

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return Scaffold(
        backgroundColor: Colors.white,
        body: Stack(
          children: [
            Container(height: 132).linearGradientColor([Color(0xffDCD4FF), Colors.white],
                begin: Alignment.topCenter, end: Alignment.bottomCenter),
            SafeArea(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      JR.img(Assets.discoverBack, width: 24, height: 24).onClick(() {
                        Get.back();
                      }),
                      SizedBox(width: 6),
                      Text(T.current.c_friend).fontSize(22).colorInt(0xff1B1B1B).fontWeight(FontWeight.w600),
                    ],
                  ).paddingSymmetric(horizontal: 0, vertical: 10).marginTop(0),
                  Row(
                    children: [
                      Row(
                        children: [
                          Image.asset(Assets.discoverSearchGray, width: 28, height: 28).marginRight(3),
                          TextField(
                            controller: logic.searchController,
                            onChanged: (value) {
                              logic.currentIndex.refresh();
                              if (value.isEmpty) logic.searchKey("");
                            },
                          )
                              .noBorder()
                              .hint(T.current.discover6_choose_friends)
                              .hintColor(Color(0xffA8A8A8))
                              .isCollapsedThenContentPaddingSymmetric(vertical: 7)
                              .expanded(),
                        ],
                      ).paddingSymmetric(horizontal: 13).backgroundColorInt(0xffF2F2F2).roundedRect(90).expanded(),
                      SizedBox(width: 9),
                      Text(T.current.c_search)
                          .colorInt(logic.searchController.text.isEmpty ? 0xffC1C1C1 : 0xFF8A72FE)
                          .fontSize(18)
                          .onClick(
                        () {
                          if (logic.searchController.text.isNotEmpty) logic.searchKey(logic.searchController.text);
                        },
                      ),
                    ],
                  ).marginBottom(16),
                  TabBar(
                    controller: logic.tabController,
                    isScrollable: true,
                    dividerColor: Colors.transparent,
                    tabAlignment: TabAlignment.start,
                    indicatorSize: TabBarIndicatorSize.label,
                    indicatorWeight: 1,
                    indicator: const BoxDecoration(color: Colors.transparent),
                    overlayColor: const WidgetStatePropertyAll(Colors.transparent),
                    padding: EdgeInsets.zero,
                    labelPadding: const EdgeInsets.only(right: 10),
                    unselectedLabelStyle: const TextStyle(color: Color(0xFFC1C1C1), fontSize: 12),
                    labelStyle: const TextStyle(color: Color(0xFF8A72FE), fontSize: 12),
                    tabs: [
                      Text(T.current.discover6_friend)
                          .colorInt(logic.currentIndex() == 0 ? 0xff8A72FE : 0xffC1C1C1)
                          .fontSize(12)
                          .paddingSymmetric(horizontal: 12, vertical: 3)
                          .backgroundColorInt(logic.currentIndex() == 0 ? 0xffffffff : 0xfff4f4f4)
                          .roundedRect(90)
                          .borderIs(color: Color(logic.currentIndex() == 0 ? 0xff8A72FE : 0xfff4f4f4), radius: 90)
                          .marginRight(16),
                      Text(T.current.discover2_following)
                          .colorInt(logic.currentIndex() == 1 ? 0xff8A72FE : 0xffC1C1C1)
                          .fontSize(12)
                          .paddingSymmetric(horizontal: 12, vertical: 3)
                          .backgroundColorInt(logic.currentIndex() == 1 ? 0xffffffff : 0xfff4f4f4)
                          .roundedRect(90)
                          .borderIs(color: Color(logic.currentIndex() == 1 ? 0xff8A72FE : 0xfff4f4f4), radius: 90)
                          .marginRight(16),
                      Text(T.current.discover3_followed)
                          .colorInt(logic.currentIndex() == 2 ? 0xff8A72FE : 0xffC1C1C1)
                          .fontSize(12)
                          .paddingSymmetric(horizontal: 12, vertical: 3)
                          .backgroundColorInt(logic.currentIndex() == 2 ? 0xffffffff : 0xfff4f4f4)
                          .roundedRect(90)
                          .borderIs(color: Color(logic.currentIndex() == 2 ? 0xff8A72FE : 0xfff4f4f4), radius: 90)
                          .marginRight(16),
                    ],
                  ),
                  SizedBox(height: 8),
                  TabBarView(
                    controller: logic.tabController,
                    children: [
                      userList(logic.friends),
                      userList(logic.followings),
                      userList(logic.fans),
                    ],
                  ).expanded(),
                ],
              ).marginOnly(top: 0, left: 16, right: 16),
            ),
          ],
        ),
      );
    });
  }

  Widget userList(RxList<FriendModel> ofriends) {
    var friends = ofriends
        .where((p0) => logic.searchKey().isEmpty || (p0.nickname?.contains(logic.searchKey()) ?? false))
        .toList();
    return Visibility(
      visible: friends.isNotEmpty,
      replacement: Column(
        children: [
          Image.asset(
            Images.bgPlaceEmpty,
            width: 200,
            height: 200,
          ).marginTop(100),
          Text(T.current.discover7_no_friend).fontSize(12).colorInt(0xffc1c1c1),
        ],
      ),
      child: ListView.builder(
        shrinkWrap: true,
        itemCount: friends.length,
        itemBuilder: (context, index) {
          var user = friends[index];
          return Column(
            children: [
              Row(
                children: [
                  JR.img(user.headPortraitsUrl ?? "", fit: BoxFit.cover, width: 54, height: 54).roundedRect(90),
                  SizedBox(width: 7),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(user.nickname ?? "").fontSize(16).marginBottom(4),
                      Text(user.about ?? "").fontSize(12).colorInt(0xffC1C1C1),
                    ],
                  )
                ],
              ).paddingSymmetric(vertical: 16),
              Container(height: 0.5, color: Color(0xffD8D8D8)),
            ],
          ).paddingSymmetric(horizontal: 16).onClick(
            () {
              Get.back(result: user);
            },
          );
        },
      ),
    );
  }
}
